package com.xrf;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.util.WebUtils;
import com.system.as.application.system.basics.file.entity.SystemFile;
import com.system.as.application.system.basics.file.service.SystemFileService;
import com.system.core.common.EnumCode;
import com.system.core.common.ResultView;
import com.system.core.tool.ToolRandom;
import io.system.annotations.Api;
import io.system.annotations.ApiOperation;

@RestController()
@RequestMapping("/file")
public class FileController {
    private static Logger logger = LoggerFactory.getLogger(FileController.class);
    
    @Autowired
    private SystemFileService fileService;
    
    @GetMapping("/prview/pdf")
    public void readFile(HttpServletResponse response, String fileIndex) {
        try {
            response.setContentType("application/pdf");
            SystemFile sf = fileService.getById(fileIndex);
            OutputStream out = response.getOutputStream();
            out.write(sf.getContent());
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            logger.error("预览结果错误，文件不存在", e);
        } catch (IOException e) {
            logger.error("预览结果未知错误", e);
        } finally {
            logger.error("预览结果-结束");
        }
    }
    
    @PostMapping("/upload")
    public ResultView insertFile(HttpServletRequest request) {
        ResultView view = new ResultView(EnumCode.SUCCESS);
        try {
            MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(request,
                    MultipartHttpServletRequest.class);
            String ext = request.getParameter("ext");
            MultipartFile multiFile = multipartRequest.getFile("file");
            String originaFilename = multiFile.getOriginalFilename();
            String fileType = originaFilename.substring(originaFilename.lastIndexOf(".") + 1);
            String fileName = ToolRandom.getStringByLen(12)
                    + (originaFilename.lastIndexOf(".") != -1 ? "." + fileType : "");
            String fileSize = multiFile.getSize() + "";
            SystemFile _sf = fileService.save(fileName, fileType, fileSize, multiFile.getBytes());
            view.setData(_sf);
        } catch (Exception e) {
            logger.error("添加附件-错误", e);
        } finally {
            logger.info("添加附件-结束");
        }
        return view;
    }
}